package com.scu.bank.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scu.bank.model.dao.UserRoleDao;
import com.scu.bank.model.example.UserRoleExample;
import com.scu.bank.model.pojo.UserRole;
import com.scu.bank.model.base.BaseServiceImpl;
import com.scu.bank.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pmeng on 2021/01/04.
 */
@Service
@Transactional
public class UserRoleServiceImpl extends BaseServiceImpl<UserRole,UserRoleDao> implements UserRoleService {
    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public UserRoleDao getDao() {
        return userRoleDao;
    }

    @Override
    public PageInfo<UserRole> list(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        UserRoleExample example = new UserRoleExample();
        example.createCriteria().andIsDelEqualTo(false);
        PageInfo pageInfo = new PageInfo(userRoleDao.selectByExample(example));
        return pageInfo;
    }

    @Override
    public List<Map<String, String>> getRoleList() {
        List<Map<String, String>> roles = new ArrayList<>();

        UserRoleExample example = new UserRoleExample();
        example.createCriteria()
                .andIsDelEqualTo(false);
        example.setOrderByClause("create_time desc");

        List<UserRole> userRoles = userRoleDao.selectByExample(example);
        for (UserRole userRole : userRoles){
            Map<String,String> pro = new HashMap<>();
            pro.put("id",userRole.getId());
            pro.put("label",userRole.getName());
            roles.add(pro);
        }
        return roles;
    }
}
